package com.example.vehiclemanagesystem.controller;

import com.example.vehiclemanagesystem.entity.Car;
import com.example.vehiclemanagesystem.entity.Driver;
import com.example.vehiclemanagesystem.entity.Route;
import com.example.vehiclemanagesystem.entity.Vehicle;
import com.example.vehiclemanagesystem.service.CarService;
import com.example.vehiclemanagesystem.service.DriverService;
import com.example.vehiclemanagesystem.service.RouteService;
import com.example.vehiclemanagesystem.service.VehicleService;
import com.example.vehiclemanagesystem.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.time.LocalTime;
import java.util.*;

@RestController
@CrossOrigin
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private DriverService driverService;

    @PostMapping("/addCar")
    public Map<String, Object> addCar(@RequestBody Map<String, String> remap) {
        Integer r_id=Integer.parseInt(remap.get("r_id"));
        Integer v_id=Integer.parseInt(remap.get("v_id"));
        Integer d_id=Integer.parseInt(remap.get("d_id"));
        String time=remap.get("time");
        String sudden_situation=remap.get("sudden_situation");
        boolean  notice=Boolean.parseBoolean(remap.get("notice"));
        Map<String, Object> map = new HashMap<>();
        try {
            Car car=new Car(r_id,v_id,d_id, DateUtil.parseDateTime(time),sudden_situation,notice);
            Route route=routeService.selectRouteByRouteID(r_id);
            Vehicle vehicle=vehicleService.selectVehicleByVehicleID(v_id);
            Driver driver=driverService.selectDriverByDriverID(d_id);
            if (route == null||vehicle==null||driver==null) {
                map.put("success", false);
                map.put("message", "车辆路线安排非法");
            } else {
                carService.addNewDCar(car);
                map.put("success", true);
                map.put("message", "车辆路线添加成功 ");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    @PostMapping("/deleteCar")
    public Map<String, Object> deleteCar(@RequestBody String input) {
        input = input.substring(0,input.length()-1);
        System.out.println(input);
        Integer c_id = Integer.parseInt(input);
        Map<String, Object> map = new HashMap<>();
        try {
            Car car1=carService.selectCarByCarID(c_id);
            if (car1 == null) {
                map.put("success", false);
                map.put("message", "该车辆路线不存在");
            } else {
                carService.deleteCar(car1);
                map.put("success", true);
                map.put("message",/*d_id+*/"车辆路线删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }
    @PostMapping("/updateCar")
    public Map<String, Object> updateCar(@RequestBody Map<String, String> remap) {
        Integer c_id=Integer.parseInt(remap.get("c_id"));
        Integer r_id=Integer.parseInt(remap.get("r_id"));
        Integer v_id=Integer.parseInt(remap.get("v_id"));
        Integer d_id=Integer.parseInt(remap.get("d_id"));
        String time=remap.get("time");
        boolean  notice=Boolean.parseBoolean(remap.get("notice"));
        String sudden_situation=remap.get("sudden_situation");
        Map<String, Object> map = new HashMap<>();
        try {
            Car car1=carService.selectCarByCarID(c_id);
            if (car1 == null) {
                map.put("success", false);
                map.put("message", "车辆路线不存在");
            } else {
                Car car=new Car(r_id,v_id,d_id, DateUtil.parseDateTime(time),sudden_situation,notice);
                car.setC_id(c_id);
                carService.updateCar(car);
                map.put("success", true);
                map.put("message",/*d_id+*/"车辆路线修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("message", "操作失败！");
        }
        return map;
    }

    @RequestMapping("/searchCars")
    public Map<String, Object> searchCars(@RequestParam("query") String input) {
        Map<String, Object> map = new HashMap<>();
        List<Car> cars = carService.selectCarAll(input);
        for(Car car:cars){
            car.setStrDate(DateUtil.formatDateTime(car.getTime()));
        }
        //Map<String, Object> map = new HashMap<>();
        map.put("pagenum", 1);
        int total = cars.size();
        map.put("total", total);
        map.put("success", true);
        map.put("整个车辆表", cars);
        return map;
    }

    @RequestMapping("/selectCarById")
    public Map<String, Object> selectCarById(@RequestParam("query") String input) {
        Map<String, Object> map = new HashMap<>();
        List<Car> cars = carService.selectCarById(input);
        //Map<String, Object> map = new HashMap<>();
        map.put("pagenum", 1);
        int total = cars.size();
        map.put("total", total);
        map.put("success", true);
        map.put("整个车辆表", cars);
        return map;
    }

}
